package com.example.musicapp.presenter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.ParcelUuid;
import android.widget.Toast;

import com.example.musicapp.application.MyApp;
import com.example.musicapp.base.BasePresenter;
import com.example.musicapp.bean.User;
import com.example.musicapp.contract.ILoginContract;
import com.example.musicapp.model.LoginModel;
import com.example.musicapp.network.HttpResponseListener;


import org.json.JSONException;
import org.json.JSONObject;

import java.lang.ref.WeakReference;


public class LoginPresenter extends BasePresenter<ILoginContract.ILoginUi> implements ILoginContract.LoginPre {
    public static final int LOGIN_SUCCESS = 11;
    public static final int  LOGIN_FAILURES = 12;
    public static final int MES_LOGIN_SUCCESS = 123;

    private static ILoginContract.ILoginUi sView ;
    private Handler mHandler = new MyHandler(this);

    private static class MyHandler extends Handler {

        private WeakReference<LoginPresenter> modelWeakReference;

        public MyHandler(LoginPresenter presenter) {

            modelWeakReference = new WeakReference<>(presenter);
        }

        @Override
        public void handleMessage(Message msg) {
            LoginPresenter presenter = modelWeakReference.get();
            if (presenter != null) {
                switch (msg.what){
                    case MES_LOGIN_SUCCESS:

                        if (presenter!=null) {
                            presenter.getView().loginSuccess((String) msg.obj);
                        }
                        break;



                }
            }
        }
    }


    public LoginPresenter(ILoginContract.ILoginUi view) {
        super(view);
    }
    private ILoginContract.LoginModel mModel = new LoginModel();
    @Override
    public int login(String username, String password) {

        if (checkScan(username, password)){

            mModel.login(username, password, new HttpResponseListener() {
                @Override
                public void onSuccess(Object tag) throws JSONException {

                    JSONObject jsonObject  = new JSONObject( (String ) tag);
                    int code = jsonObject.getInt("code");
                    Message message = mHandler.obtainMessage();

                    if (code == 200){

                        JSONObject jsonObject1 = jsonObject.getJSONObject("profile");
                        String username = jsonObject1.getString("nickname");
                        String cookie = jsonObject.getString("cookie");
                        saveLoginMessage(username,cookie);
                        message.obj = username;
                        message.what = MES_LOGIN_SUCCESS;

//                        getView().loginSuccess(username);

                    }
                    else {

//                        getView().loginFailure();
                    }
                    mHandler.sendMessage(message);

                }

                @Override
                public void onFailure(Object tag, Object failure) {

                }
            });
        }
        return 0;
    }


    private boolean checkScan(String username,String password){
        if ("".equals(username)||"".equals(password)){
            Toast.makeText(MyApp.getContext(),"请不要留空",Toast.LENGTH_SHORT).show();
            return false;
        }
        if (username.contains(" ")){
            Toast.makeText(MyApp.getContext(),"请不要在账号上输入空格",Toast.LENGTH_SHORT).show();
            return false;
        }
    return true;
    }


    private void saveLoginMessage(String username,String cookie){
        SharedPreferences.Editor editor = MyApp.getContext().getSharedPreferences("DATA", Context.MODE_PRIVATE).edit();
        editor.putString("username",username);
        editor.putString("cookie",cookie);
        editor.apply();
    }
}
